package com.cn.kehong.controller;


import com.cn.kehong.api.IDepartmentService;
import com.cn.kehong.domain.Department;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/dept")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @ResponseBody
    @RequestMapping("/getDepartmentListTree/{frontToken}")
    public JsonResponse<List<Department>> getDepartmentListTree(QueryForm queryForm,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<Department>> response = new JsonResponse<List<Department>>();
        List<Department> departments = departmentService.getDepartmentListTree(queryForm);
        response.setResult(departments);
        response.setSuccess(true);
        return response;
    }

    @ResponseBody
    @RequestMapping("/addDeptDetail/{frontToken}")
    public JsonResponse<String> addDeptDetail(Department department,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = departmentService.addDeptDetail(department);
        return CommonUtil.getResponseData(response,result);
    }

    @ResponseBody
    @RequestMapping("/updateDeptDetail/{frontToken}")
    public JsonResponse<String> updateDeptDetail(Department department,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = departmentService.updateDeptDetail(department);
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/queryDeptChooseList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Department>> queryDeptChooseList(String id,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<Department>> response = new JsonResponse<List<Department>>();
        List<Department> list  = departmentService.queryDeptChooseList(id);
        response.setResult(list);
        response.setSuccess(true);
        return  response;
    }

    @ResponseBody
    @RequestMapping("/deleteDeptDetail/{frontToken}")
    public JsonResponse<String> deleteDeptDetail(@RequestBody QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = departmentService.deleteDeptDetail(queryForm);
        return CommonUtil.getResponseData(response,result);
    }


}
